package com.cx.mall.goods.service.impl;


import com.cx.mall.common.entity.goods.entity.SkuInfo;
import com.cx.mall.common.entity.order.model.Stock;
import com.cx.mall.goods.mapper.SkuInfoMapper;
import com.cx.mall.goods.service.ISkuInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author zs
 * @since 2025-08-07
 */
@Service
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo> implements ISkuInfoService {
    @Autowired
    private SkuInfoMapper skuInfoMapper;
    @Override
    public void doCount(Stock stock) {
        if(stock.getNum() > 0 && stock.getSkuId() != null){
            SkuInfo skuInfo = skuInfoMapper.selectById(stock.getSkuId());
            if(skuInfo.getNum() < stock.getNum()){
                throw new RuntimeException("库存不足");
            }
            skuInfo.setNum(skuInfo.getNum() - stock.getNum());
            skuInfoMapper.updateById(skuInfo);
        }else {
            throw new RuntimeException("库存扣减失败");
        }


    }
}
